import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

import '../center_title_app_bar.dart';
import 'model/radio_item_data.dart';
import 'radio_list.dart';

Future<List<T>> showRadioListPicker<T>(
  BuildContext context, {
  @required String title,
  @required List<RadioItemData<T>> items,
  List<T> value,
  bool isMultiple = false,
}) {
  return Navigator.push<List<T>>(
      context,
      MaterialPageRoute(
          builder: (context) => _RadioListPicker<T>(
                title: title,
                items: items,
                value: value,
                isMultiple: isMultiple,
              )));
}

class _RadioListPicker<T> extends StatefulWidget {
  final List<RadioItemData<T>> items;
  final List<T> value;
  final String title;
  final bool isMultiple;

  const _RadioListPicker({
    Key key,
    this.items,
    this.value,
    this.title,
    this.isMultiple,
  }) : super(key: key);

  @override
  __RadioListPickerState<T> createState() => __RadioListPickerState<T>();
}

class __RadioListPickerState<T> extends State<_RadioListPicker<T>> {
  List<T> value;

  @override
  void initState() {
    super.initState();

    value = widget.value;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(
        title: widget.title,
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: RadioList<T>(
              items: widget.items,
              value: value ?? [],
              isMultiple: widget.isMultiple,
              padding: EdgeInsets.symmetric(
                horizontal: 50.w,
              ),
              onChanged: (value) {
                setState(() {
                  this.value = value;
                });
              },
            ),
          ),
          FixedBottomBtn(
            title: '保 存',
            onTap: () {
              Navigator.pop(
                context,
                value,
              );
            },
          ),
        ],
      ),
    );
  }
}
